package com.hmy.finance.job.receipt;

import com.google.gson.Gson;
import com.hmy.finance.service.core.IDeviceDailyBillService;
import com.hmy.finance.service.core.IProfitLossRecordService;
import com.hmy.finance.service.core.IReceivableInventoryService;
import com.hmy.finance.service.receipt.OffsetFeeBillService;
import com.hmy.finance.service.receipt.dto.offset.AutomaticOffsetDto;
import com.hmy.finance.service.receipt.rdto.offset.AutomaticOffsetRdto;
import com.xxl.job.core.context.XxlJobHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * 自动核销冲抵任务执行类
 *
 * @author songkf
 */
@Slf4j
@Component
public class AutoOffsetFeeBillJobHandler {
    @Resource
    private IDeviceDailyBillService deviceDailyBillService;
    @Resource
    private OffsetFeeBillService offsetFeeBillService;
    @Resource
    private IReceivableInventoryService receivableInventoryService;
    @Resource
    private IProfitLossRecordService profitLossRecordService;
    /**
     * Define the thread pool for the auto offset fee bill job
     */
    private static final ThreadPoolExecutor THREAD_POOL = new ThreadPoolExecutor(20, 20, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<>());

    /**
     * Execute the auto offset fee bill job
     *
     * @param customerId the ID of the customer
     * @param contractId the ID of the sale contract
     * @param startDate  the start date for the daily bill
     * @param endDate    the end date for the daily bill
     * @return the execution result
     * @author songkf
     */
    public void execute() {
        String param = XxlJobHelper.getJobParam();
        Gson gson = new Gson();
        AutomaticOffsetDto autoOffsetDto = gson.fromJson(param, AutomaticOffsetDto.class);
        log.info("AutoOffsetFeeBillJobHandler:execute:{}", autoOffsetDto);
        AutomaticOffsetRdto automaticOffsetRdto = offsetFeeBillService.automaticOffset(autoOffsetDto);
        log.info("AutoOffsetFeeBillJobHandler:Response:{}", automaticOffsetRdto);
    }
}
